// components/vote/index.js
import { Topic } from "../../model/Topic";
import { Utils } from "../../utils/util";
const topic = new Topic();
const utils = new Utils();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        topic: {
            type: Object,
            value: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        voteId: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /**
         * 选择投票项
         */
        checkVote(e) {
            if (this.data.topic.isExpire || this.data.topic.LoggerCanVote == 0) {
                return false;
            }
            const i = e.currentTarget.dataset.index;
            let votes = this.data.topic.votes;
            let newVotes = votes.map((vote, index) => {
                if (this.data.topic.VoteType == 1) {
                    vote.checked = false;
                    if (index == i) {
                        vote.checked = true;
                        this.setData({
                            voteId: [vote.VoteId]
                        });
                    }
                } else {
                    if (index == i) {
                        const state = vote.checked;
                        vote.checked = !state;
                        let voteIds = this.data.voteId;
                        if (state) {
                            let newVoteIds = voteIds.filter((id) => {
                                return id != vote.VoteId;
                            });
                            this.setData({
                                voteId: newVoteIds
                            });
                        } else {
                            voteIds.push(vote.VoteId);
                            this.setData({
                                voteId: voteIds
                            });
                        }
                    }
                }
                return vote;
            });
            this.setData({
                ['topic.votes']: newVotes
            });
        },
        /**
         * 确认投票
         */
        subVote() {
            if (this.data.topic.isExpire || this.data.topic.LoggerCanVote == 0) {
                return false;
            }
            if (this.data.voteId.length == 0) {
                wx.showToast({
                    title: '请选择投票项',
                    icon: "none"
                })
                return false;
            }
            let options = {
                postId: this.data.topic.PostId,
                voteId: this.data.voteId.join(",")
            }
            topic.vote(options)
                .then(res => {
                    wx.showToast({
                        title: '投票成功',
                    })
                    this.triggerEvent("vote", {
                        postId: this.data.topic.PostId,
                        voteId: this.data.voteId
                    });
                })
                .catch(err => {
                    wx.showToast({
                        title: err.msg,
                        icon: "none"
                    })
                });
        },
        /**
         * 预览投票图片
         */
        preViewVoteImg(e) {
            const url = e.currentTarget.dataset.url;
            utils.previewImage(0, [url]);
        }
    }
})